package top.xiaoshuaila.searescue.task;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import top.xiaoshuaila.searescue.pojo.Buoy;
import top.xiaoshuaila.searescue.pojo.Helmet;
import top.xiaoshuaila.searescue.pojo.Serial;
import top.xiaoshuaila.searescue.pojo.Tag;
import top.xiaoshuaila.searescue.service.*;
import top.xiaoshuaila.searescue.util.CoordinateConversion;

import java.util.HashMap;
import java.util.Map;

@Component
public class SerialTask {

    @Autowired
    private AfterWebSocket afterWebSocket;
    @Autowired
    private UWBWebSocket uwbWebSocket;
    @Autowired
    private MqttDataDeal2 mqttDataDeal;
    @Autowired
    private SerialPortservice serialPortservice;
    @Autowired
    private Serial serial;
    @Autowired
    private Buoy buoy;
    @Autowired
    private Tag tag;
    @Autowired
    private DataDeal dataDeal;
    @Autowired
    private MqttService mqttService;
    @Scheduled(cron = "0/1 * * * * ?")
    private void open(){
        if (serial.getSystemStatus()!=0) {
            String[] msg = new String[0];
            if(!serial.isIsopen())
            serialPortservice.openSerialPort(msg);
        } else {
            serialPortservice.closeSerialPort();
        }
        if(serial.getSystemStatus()==2&&!buoy.isSTOP()&&!buoy.isShoudong()&&!buoy.isSTOPSTOP())
        {
            buoy.setTIME(buoy.getTIME()+1);
        }
        else if(buoy.isShoudong())
        {
            buoy.setTIME(0);
        }
    }
    @Scheduled(cron = "0/4 * * * * ?")
    private void sendTask(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                JSONObject json=new JSONObject();
                json.put("tag",serial.getSystemStatus());
                json.put("tagid",tag.getTAG());
                json.put("Lost",mqttService.lost);
                Map<Integer, Helmet>mapmap= mqttDataDeal.map;
                for(Integer a:mapmap.keySet())
                {
                    json.put(a+"",mapmap.get(a));
                }
                uwbWebSocket.sendMessage(json.toString());
                if(serial.getSystemStatus()==0)
                {
                    System.out.println("安全模式");
                    JSONObject jsonObject=new JSONObject();
                    jsonObject.put("tag","safe");
                    jsonObject.put("status",0);
                    afterWebSocket.sendMessage(jsonObject.toString());
                }
                else if(serial.getSystemStatus()==1)
                {
                    dataDeal.STTGetStatus();
                    System.out.println("警报模式");
                    JSONObject jsonObject=new JSONObject();
                    jsonObject.put("tag","alert");
                    HashMap<String,Object>map=new HashMap<>();
                    map.put("tagId",tag.getTAG());
                    double[] point =
                            CoordinateConversion.wgs84tobd09(tag.getLongitude(), tag.getLatitude());
                    map.put("tagX",point[0]);
                    map.put("tagY",point[1]);
                    map.put("tagS",tag.getSOG());
                    map.put("tagL",tag.getDIS());
                    jsonObject.put("Tag",map);
                    jsonObject.put("status",1);
                    afterWebSocket.sendMessage(jsonObject.toString());
                }
                else{
                    System.out.println("救援模式");
                    //向救生圈发
                    if(!buoy.isLuoshui());
                    else if(tag.getLatitude()==-1)
                    {
                        dataDeal.STHStop();
                    }
                    else if(buoy.isSTOPSTOP())
                    {
                        dataDeal.STHStop();
                    }
                    else if(buoy.isSTOP())
                    {
                        dataDeal.STHStop();
                    }
                    else if(buoy.isShoudong());
                    else if(buoy.isRTN())
                    {
                         dataDeal.STHReturns();
                    }
                    else if(buoy.getSTATUS().equals("OK"))
                    {
                        dataDeal.STHPoint();
                    }
                    else if(buoy.getSTATUS().equals("ACQ"))
                    {
                        dataDeal.STHStop();
                    }
                    else if(buoy.getSTATUS().equals("RTN"))
                    {
                        if(buoy.getMOTOR().equals("IDLE"))
                            dataDeal.STHStop();
                        else dataDeal.STHReturns();
                    }
                    System.out.println(buoy);
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    //向标签发
                    dataDeal.STTGetStatus();

                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    JSONObject jsonObject=new JSONObject();
                    jsonObject.put("tag","help");
                    HashMap<String,Object> map=new HashMap<>();
                    map.put("buoyId",buoy.getBOAT());
                    if(buoy.getSTATUS().equals("OK"))
                    {
                        if(buoy.isShoudong())
                        map.put("buoyS","手动控制中");
                        else map.put("buoyS","救援中");
                    }
                    else if(buoy.getSTATUS().equals("ACQ"))
                    {
                        map.put("buoyS","寻星中");
                    }
                    else if(buoy.getSTATUS().equals("RTN")||buoy.isRTN())
                    {
                        map.put("buoyS","返航中");
                    }
                    if(!buoy.isLuoshui())
                        map.put("buoyS","未就绪");
                    double[] point =
                            CoordinateConversion.wgs84tobd09(buoy.getLongitude(), buoy.getLatitude());
                    map.put("buoyX",point[0]);
                    map.put("buoyY",point[1]);
                    map.put("buoyV",buoy.getVOLT());
                    map.put("Stop",buoy.isSTOP());
                    map.put("MOTOR",buoy.getMOTOR());
                    map.put("Lost",buoy.getTIME()>30);
                    jsonObject.put("Buoy",map);
                    map=new HashMap<>();
                    map.put("tagId",tag.getTAG());
                    point =
                            CoordinateConversion.wgs84tobd09(tag.getLongitude(), tag.getLatitude());
                    map.put("tagX",point[0]);
                    map.put("tagY",point[1]);
                    map.put("tagS",tag.getSOG());
                    map.put("tagL",tag.getDIS());
                    jsonObject.put("Tag",map);
                    jsonObject.put("status",2);
                    jsonObject.put("shoudong",buoy.isShoudong());
                    System.out.println(jsonObject);
                    afterWebSocket.sendMessage(jsonObject.toString());
                }
            }
        }).start();
    }
}
